#include <REGX52.H>
#include "LCD1602.h"
#include "Time0.h"
#include "Delay.h"

void pauseOrContinue();

unsigned char sec, min, hour;
unsigned int sign = 0;

// 计时器（1602显示）

void main() {
	
	LCD_Init();
	Timer0Init();
	
	LCD_ShowString(1,1,"Clock");
	LCD_ShowString(2,3,":");
	LCD_ShowString(2,6,":");
	
	while (1) {
		
		LCD_ShowNum(2,7,sec,2);
		
		LCD_ShowNum(2,4,min,2);
		
		LCD_ShowNum(2,1,hour,2);
		
		
	}
	
}

void Timer0_Routine() interrupt 1 {
	
	static unsigned int T0Count;
	
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	
	pauseOrContinue();
	
	if (sign == 1) {
		T0Count++;
	}
	if (T0Count>=1000) {
		T0Count = 0;
			sec++;
			if (sec == 60) {
				sec = 0;
				min++;
				if (min == 60) {
					min = 0;
					hour++;
					if (hour == 24) {
						hour = 0;
					}
				}
			}
		
		
	}
	
}

void pauseOrContinue() {
	
	if (P3_1 == 0){
		Delay(20); 
		while(P3_1 == 0);
		Delay(20); 
		if (sign == 1) {
			sign = 0;
		} else {
			sign = 1;
		}
	}
}